package com.huawei.statistics.jdong;

import com.huawei.statistics.pojo.video.TVPlay;
import com.huawei.statistics.pojo.video.Video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by 18573 on 2018/12/8.
 */
public class VideoParse {

    public static List<TVPlay> getTVPlayByVideo(String html,Video video){
        List<TVPlay> list = new ArrayList<>();

        Document doc = Jsoup.parse(html);
        Elements elements=doc.select("div[class=piclist-wrapper]").select("ul").select("li[data-albumlist-elem=playItem]");
        for(Element element : elements){

            String url = element.select("a[class=site-piclist_pic_link]").attr("href");
            String title = element.select("a[class=site-piclist_pic_link]").attr("title");
            String image = element.select("a[class=site-piclist_pic_link]").select("img").attr("src");
            String episode = element.select("p[class=site-piclist_info_title]").select("a").text();
            String time = element.select("div[class=wrapper-listTitle]").select("div[class=mod-listTitle]").select("span").text();

            TVPlay play = new TVPlay();
            play.setCreateDate(new Date());
            play.setEpisode(episode);
            play.setTitle(video.getTitle());
            play.setUrl(url);
            play.setImage(image);
            play.setTime(time);
            play.setVideoId(video.getId());
            list.add(play);
        }
        System.out.println(video.getTitle()+"======"+list.size());
        return list;
    }

    //电影 type = 1 电影   2 电视剧 4 动漫
    public static List<Video> getVideoList(String html,int type){

        List<Video> list = new ArrayList();
        Document doc = Jsoup.parse(html);
        Elements elements=doc.select("ul[class=site-piclist site-piclist-180236 site-piclist-auto]").select("li");
        for(Element element:elements){

            String url =element.select("a").attr("href");
            String imageulr = element.select("img").attr("src");
            String title = element.select("a").attr("title");
            String vid = element.select("a").attr("data-qipuid");
            String time = element.select("span[class=icon-vInfo]").text().trim();
            String autor = element.select("div[class=role_info] a[data-searchpingback-elem=link]").text().replace("\r\n","");
            if(autor.length() >20){
                autor = autor.substring(0,20)+"...";
            }
            Video video = new Video();
            video.setVid(vid);
            video.setTitle(title);
            video.setImage(imageulr);
            video.setUrl(url);
            video.setDate(new Date());
            video.setDirector(autor);
            video.setTime(time);
            video.setSource("爱奇艺");
            video.setImage(imageulr);
            video.setType(type);
            video.setKeywords(title+"免费观看,"+title+"在线观看,"+title+"下载");
            video.setDescri("");
            list.add(video);
        }

        return  list;

    }





}
